package com.pocket.insight.base.dto;

import lombok.Data;
import org.slf4j.MDC;

/**
 * @author shuize
 */
@Data
public class Result<T> {

    private String requestId;
    private long code;
    private String msg;
    private T data;


    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return createResult("", ResponseCodeEnum.SUCCESS.getCode(), data);
    }

    public static <T> Result<T> success(long code, T data) {
        return createResult("", code, data);
    }
    public static <T> Result<T> success(String message, long code) {
        return createResult(message, code, null);
    }

    public static <T> Result<T> error() {
        return error(null);
    }

    public static <T> Result<T> error(String message) {
        return error(ResponseCodeEnum.ERROR.getCode(), message);
    }

    public static <T> Result<T> error(long code, T data) {
        return createResult(null, code, data);
    }

    public static <T> Result<T> error(long code, String message) {
        return createResult(message, code, null);
    }

    public static <T> Result<T> error(long code, String message, T data) {
        return createResult(message, code, data);
    }

    public static <T> Result<T> error(T data) {
        return error(null, data);
    }

    public static <T> Result<T> error(String message, T data) {
        return createResult(message, ResponseCodeEnum.ERROR.getCode(), data);
    }


    private static <T> Result<T> createResult(String message, long code, T data) {
        Result<T> r = new Result();
        r.setRequestId(MDC.get("traceId"));
        r.setCode(code);
        r.setMsg(message);
        r.setData(data);
        return r;
    }

}
